﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimbreRecognition.Util
{
    public class DataUtil
    {
        public static List<Feature> Randomize(List<Feature> data)
        {
            List<Feature> copy = new List<Feature>(data);

            Random random = new Random();

            List<Feature> randomized = new List<Feature>();

            while (copy.Count > 0)
            {
                int index = random.Next(copy.Count);
                Feature entry = copy[index];
                randomized.Add(entry);
                copy.RemoveAt(index);
            }

            return randomized;
        }

        public static Dictionary<double[], double[]> Randomize(Dictionary<double[], double[]> data)
        {
            Dictionary<double[], double[]> copy = new Dictionary<double[], double[]>(data);

            Random random = new Random();

            Dictionary<double[], double[]> randomized = new Dictionary<double[], double[]>();

            while (copy.Count > 0)
            {
                int index = random.Next(copy.Count);
                double[] key = copy.Keys.ElementAt(index);
                double[] value = copy.Values.ElementAt(index);
                randomized.Add(key, value);
                copy.Remove(key);
            }

            return randomized;
        }

        public static float[] ApproximateArray(float[] array, int newSize)
        {
            float[] newArray = new float[newSize];
            int increment = array.Length / newSize;
            int dataIndex = 0;
            int index = 0;
            while (index < newSize - 1)
            {
                newArray[index++] = array[dataIndex];
                dataIndex += increment;
            }
            return newArray;
        }

        public static List<float[]> Split(float[] data, int batchSize)
        {
            int index = 0;
            List<float[]> batches = new List<float[]>();
            while (index + batchSize < data.Length - 1)
            {
                float[] batch = new float[batchSize];
                Array.Copy(data, index, batch, 0, batchSize);
                batches.Add(batch);
                index += batchSize;
            }

            return batches;
        }
    }
}
